/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.source.network.interceptor;

import com.logos.common.source.network.callback.HttpRespondResultCallBack;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author Young Ken
 */

public class RequestComInterceptor implements Interceptor {

    private HttpRespondResultCallBack mHttpRespondResultCallBack;

    public RequestComInterceptor() {
    }

    public void setHttpRespondResultCallBack(HttpRespondResultCallBack httpRespondResultCallBack) {
        this.mHttpRespondResultCallBack = httpRespondResultCallBack;
    }

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        Request request = builder.build();
        if (request != null && mHttpRespondResultCallBack != null) {
            mHttpRespondResultCallBack.setRequest(request);
        }
        Response response = chain.proceed(request);
        if (response != null && mHttpRespondResultCallBack != null) {
            mHttpRespondResultCallBack.setResponse(response);
        }
        return response;
    }

    /**
     * create RequestComInterceptor
     *
     * @return RequestComInterceptor
     */
    public static RequestComInterceptor create() {
        return new RequestComInterceptor();
    }
}
